package com.shangcheng.search.controller;


import com.shangcheng.orders.feign.CartFeign;
import com.shangcheng.orders.feign.OrderFeign;
import com.shangcheng.orders.pojo.Order;
import com.shangcheng.orders.pojo.OrderItem;
import com.shangcheng.users.feign.AddressFeign;
import com.shangcheng.users.pojo.Address;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/worder")
public class OrderController {

    @Autowired
    private AddressFeign addressFeign;

    @Autowired
    private CartFeign cartFeign;

    @Autowired
    private OrderFeign orderFeign;

    @RequestMapping("/ready/order")
    public String readyOrder(@RequestParam String username,
                             Model model) {
        //收件人的地址信息
        List<Address> addressList = addressFeign.list(username).getData();
        model.addAttribute("address", addressList);

        //购物车信息
        Map map = cartFeign.list(username);
        Map data = (Map) map.get("data");
        model.addAttribute("carts", data.get("orderItemList"));
        model.addAttribute("totalNum", data.get("totalNum"));
        model.addAttribute("totalMoney", data.get("totalMoney"));
        model.addAttribute("username", username);

        //默认收件人信息
        for (Address address : addressList) {
            if ("1".equals(address.getIsDefault())) {
                //默认收件人
                model.addAttribute("deAddr", address);
                break;
            }
        }
        return "order";
    }


    @GetMapping("/add")
    public String add(@RequestParam String username, Model model) {
        Result<Order> result = orderFeign.add(username);
        //获取到订单的相关信息
        Long id = result.getData().getId();
        model.addAttribute("orderId", id);
        model.addAttribute("payMoney", result.getData().getPayMoney());
        return "pay";
    }
}
